package com.china08.yxyapi.api.home.kecheng.xiaoben;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.lessons.YxyLessonsSchoolCatalog;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyLessonsSchoolCatalogRepository;

/**
 * 校本课程--列表页--二级分类名称
 * 
 * @author LiuWeidong
 *
 */
@CrossOrigin
@RestController
public class KechengXiaobenTwoController extends YxyApiBaseController {

    @Autowired
    private YxyLessonsSchoolCatalogRepository yxyLessonsSchoolCatalogRepository;

    @RequestMapping(value = "/api_yw/home/kecheng/xiaoben/two/srh", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> precess(HttpServletRequest request, HttpServletResponse response,
            @RequestBody ReqModel reqModel) {

        String oneId = reqModel.oneId;
        String schoolIdOne = reqModel.schoolIdOne;

        List<RespModel> returnLst = new ArrayList<>();
        RespModel aa = new RespModel();
        aa.twoId = "";
        aa.twoName = "不限";
        returnLst.add(aa);

        if (!StringUtils.isNotBlank(oneId)) {
            List<YxyLessonsSchoolCatalog> yxyLessonsSchoolCatalogLst1 = yxyLessonsSchoolCatalogRepository
                    .findBySchoolIdAndLevelAll(schoolIdOne, 2);
            for (YxyLessonsSchoolCatalog yxyLessonsSchoolCatalog : yxyLessonsSchoolCatalogLst1) {
                RespModel model = new RespModel();
                model.twoId = yxyLessonsSchoolCatalog.getId();
                model.twoName = yxyLessonsSchoolCatalog.getName();
                model.schoolIdTwo = yxyLessonsSchoolCatalog.getSchoolId();
                returnLst.add(model);

            }
        }

        else {
            List<YxyLessonsSchoolCatalog> yxyLessonsSchoolCatalogLst = yxyLessonsSchoolCatalogRepository
                    .findBySchoolIdAndLevelAndPidAll(schoolIdOne, 2, oneId);
            for (YxyLessonsSchoolCatalog yxyLessonsSchoolCatalog : yxyLessonsSchoolCatalogLst) {
                RespModel model = new RespModel();
                model.twoId = yxyLessonsSchoolCatalog.getId();
                model.twoName = yxyLessonsSchoolCatalog.getName();
                model.schoolIdTwo = yxyLessonsSchoolCatalog.getSchoolId();
                returnLst.add(model);

            }
        }

        return returnLst;
    }

    /* 请求参数 */
    protected static class ReqModel extends YxyApiReqModel {

        // 一级分类ID
        public String oneId;
        // 一级学校ID
        public String schoolIdOne;

    }

    /* 响应参数 */
    protected static class RespModel extends YxyApiRespModel {

        // 二级目录ID
        public String twoId;
        // 二级目录名称
        public String twoName;
        // 二级学校ID
        public String schoolIdTwo;

    }
}
